local skel = fk.CreateSkill {
  name = "lb__mengyin",
}

Fk:loadTranslationTable {
  ["lb__mengyin"] = "梦洇",
  --[":lb__mengyin"] = "每名角色的回合结束时，你可令其重铸所有手牌并复原武将牌，然后其可对你依次使用因此进入弃牌堆的牌。",
  --9.30
  [":lb__mengyin"] = "每名角色的回合结束时，你可令其重铸所有手牌并复原武将牌，然后其可对你使用任意张因此进入弃牌堆的牌，你因此受到伤害后，你摸一张牌。",

  ["#lb__mengyin"] = "梦洇：你可令 %dest 重铸所有手牌并复原武将牌，然后其可对你依次使用因此进入弃牌堆的牌",
  --["#lb__mengyin_ask"] = "梦洇：你可以对 %dest 依次使用因〖梦洇〗进入弃牌堆的牌",
  ["#lb__mengyin_use"] = "梦洇：你可以对 %dest 使用任意张因〖梦洇〗进入弃牌堆的牌",

  ["$lb__mengyin1"] = "凝视过去的时候，过去也在凝视你。",
  ["$lb__mengyin2"] = "天亮了，该醒了。",
  ["$lb__mengyin3"] = "嘘~晚安。",
}


skel:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and not target:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__mengyin::" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = target:getCardIds("h")
    room:recastCard(cards, target, skel.name)
    target:reset()
    --[[
    if player.dead or target.dead or not room:askToSkillInvoke(target, { skill_name = skel.name, prompt = "#lb__mengyin_use::" .. player.id }) then
      return
    end
    --]]
    repeat
      local ids = table.filter(cards, function(id)
        return table.contains(room.discard_pile, id) and
            target:canUseTo(Fk:getCardById(id), player, { bypass_times = true, bypass_distances = true })
      end)
      if #ids > 0 then
        local use = room:askToUseRealCard(target, {
          pattern = ids,
          skill_name = skel.name,
          extra_data = { expand_pile = ids, exclusive_targets = { player.id }, bypass_times = true, bypass_distances = true },
          cancelable = true,
          prompt = "#lb__mengyin_use::" .. player.id,
          skip = true,
        })
        if use then
          use.extra_data = {lb__mengyin = player.id}
          table.removeOne(cards, use.card.id)
          room:useCard(use)
        else
          break
        end
        --[[
        local card = Fk:getCardById(ids[1])
        room:useCard{
          from = target,
          tos = {player},
          card = card,
          extra_data = {lb__mengyin = player.id},
        }
        table.removeOne(cards, ids[1])
        --]]
      end
    until #ids == 0 or target.dead or player.dead
  end,
})

skel:addEffect(fk.Damaged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true) --父事件
    return player:hasSkill(skel.name) and target == player and parent and parent.data
    and parent.data.extra_data and parent.data.extra_data.lb__mengyin == target.id
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

return skel
